﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace ZHDJ.WebPage
{
    public class PortalUtil
    {
        public static string GetHtmlText(string htmlScript)
        {
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(htmlScript);

            if (htmlDocument.DocumentNode != null)
            { return GetTextFromHtmlNode(htmlDocument.DocumentNode).Replace("<", "&lt;").Replace(">", "&gt;"); }

            return string.Empty;
        }

        private static string GetTextFromHtmlNode(HtmlNode htmlNode)
        {
            using (StringWriter stringWriter = new StringWriter())
            {
                GetHtmlNodeText(htmlNode, stringWriter);
                stringWriter.Flush();
                return stringWriter.ToString();
            }
        }

        private static void GetHtmlNodeText(HtmlNode htmlNode, TextWriter textWriter)
        {
            string html;
            switch (htmlNode.NodeType)
            {
                case HtmlNodeType.Comment:
                    // don't output comments
                    break;

                case HtmlNodeType.Document:
                    foreach (HtmlNode subnode in htmlNode.ChildNodes)
                        GetHtmlNodeText(subnode, textWriter);
                    break;

                case HtmlNodeType.Text:
                    // script and style must not be output
                    string parentName = htmlNode.ParentNode.Name;
                    if ((parentName == "script") || (parentName == "style"))
                        break;

                    // get text
                    html = ((HtmlTextNode)htmlNode).Text;

                    // is it in fact a special closing node output as text?
                    if (HtmlNode.IsOverlappedClosingElement(html))
                        break;

                    // check the text is meaningful and not a bunch of whitespaces
                    if (html.Trim().Length > 0)
                        textWriter.Write(HtmlEntity.DeEntitize(html));
                    break;

                case HtmlNodeType.Element:
                    switch (htmlNode.Name)
                    {
                        case "p":
                            // treat paragraphs as crlf
                            textWriter.Write("\r\n");
                            break;
                    }

                    if (htmlNode.HasChildNodes)
                        foreach (HtmlNode subnode in htmlNode.ChildNodes)
                            GetHtmlNodeText(subnode, textWriter);
                    break;
            }
        }
        /// <summary>
        /// 写字符串
        /// </summary>
        /// <param name="page"></param>
        /// <param name="strHtml"></param>
        public static void WriteString(Page page, string strHtml)
        {
            page.Response.Clear();
            page.Response.Write(strHtml);
            page.Response.End();
        }

        /// <summary>
        /// 获取短字符
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetShortString(object obj, int length)
        {
            if (obj != null)
                if (obj.ToString().Length > length)
                    return obj.ToString().Substring(0, (length - 2)) + "...";
                else
                    return obj.ToString();
            else
                return "";
        }
    }
}
